// pages/forecast/forecastDetail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scoreArr:[],
    result: 0,
    showModal: false,
    modalContent: '',
    tests: [
      {
        subject: '您的年龄区域？',
        stems: [
          {name: '小于40【0分】'},
          {name: '40-49【1分】'},
          {name: '50-59【2分】'},
          {name: '大于60【3分】'}
        ]
      },
      {
        subject: '您的性别？',
        stems: [
          {name: '女【0分】'},
          {name: '男【1分】'},
        ]
      },
      {
        subject: '如果您是女性(男性选否)，是否曾确诊为妊娠期糖尿病？',
        stems: [
          {name: '否【0分】'},
          {name: '是【1分】'},
        ]
      },
      {
        subject: '您的父母兄弟中是否患有糖尿病？',
        stems: [
          {name: '否【0分】'},
          {name: '是【1分】'},
        ]
      },
      {
        subject: '您是否曾诊断有高血压？',
        stems: [
          {name: '否【0分】'},
          {name: '是【1分】'},
        ]
      },
      {
        subject: '您是否有规律的体育锻炼？',
        stems: [
          {name: '是【0分】'},
          {name: '否【1分】'},
        ]
      },
    ],
  },

  radioChange: function (e) {
    // 按顺序把答案对应的value存放到数组中
    let nums = e.detail.value.split('-');
    this.data.scoreArr[nums[0]] = nums[1];
  },

  // 完成问卷按钮
  submitTest() {
    let score = this.data.scoreArr;
    let max = this.data.tests.length;
    if(this.isEmpty(score, max)) {
      this.calScore(score);
    }
  },

  // 判断是否有未做的题
  isEmpty(score, max) {
    for (let i = 0; i < max; i++) {
      if (score[i] === undefined) {
        this.setData({
          showModal: true,
          modalContent: '请完成所有题目!'
        })
        return false;
      }
    }
    return true;
  },

  calScore(scoreArr) {
    this.data.result = 0;
    scoreArr.forEach(num => {
      this.data.result += parseInt(num);
    });
    let result = this.data.result;
    if (result <= 2) {
      result = '您患2型糖尿病的风险为低危'
    } else if (result > 5) {
      result = '您患2型糖尿病的风险为高危'
    } else {
      result = '您患2型糖尿病的风险为中危'
    }
    this.setData({
      showModal: true,
      modalContent: result
    })
  },

  hideModal(e) {
    this.setData({
      showModal: false,
    })
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})